package com.hui.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.hui.server.entity.Department;
import com.hui.server.mapper.DepartmentMapper;
import com.hui.server.service.DepartmentService;
import com.hui.server.util.Result;
import com.mysql.cj.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hui
 * @since 2022-03-31
 */
@Service
@Slf4j
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    private DepartmentMapper departmentMapper;

    @Autowired
    public DepartmentServiceImpl(DepartmentMapper departmentMapper) {
        this.departmentMapper = departmentMapper;
    }

    /**
     * 查询所有部门
     *
     * @return
     */
    @Override
    public Result<?> getAllDepartments() {
        List<Department> list = departmentMapper.getAllDepartment(-1);
        return Result.success(list);
    }

    /**
     * 添加部门，开启事务，Isolation.SERIALIZABLE：设置隔离级别，rollbackFor：回滚的异常条件
     *
     * @param department
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.SERIALIZABLE,rollbackFor = Exception.class)
    public Result<?> addDep(Department department) throws SQLException {
        if (StringUtils.isNullOrEmpty(department.getName())) {
            log.error("客户端传入的部门名称为空：{}",department);
            return Result.error(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()),"部门名称必须不为空！");
        }
        //默认启用，数据库已经默认启用了
//        department.setIsParent(true);
        departmentMapper.addDep(department);
        if (department.getResult()==1){
            log.info("添加部门成功，新增部门ID：{}",department.getId());
            return Result.success(department.getId());
        }

        throw new SQLException("添加部门失败，存储过程执行失败，请检查数据库或客户端传入的数据："+department);

    }

    /**
     * 删除部门，开启事务
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.SERIALIZABLE,rollbackFor = Exception.class)
    public Result<?> deleteDep(Integer id) throws SQLException {

        Department department = new Department().setId(id);
        log.info("执行删除");
        departmentMapper.deleteDep(department);
        log.info(department.toString());

        if (-1==department.getResult()){
            log.error("无法删除，result：{}",department.getResult());
            return Result.error(String.valueOf(HttpStatus.NOT_MODIFIED.value()),"无法删除，该部门拥有子类联系或与其他表有联系");
        }
        if (department.getResult()==1){
            return Result.success("删除成功！");
        }
        throw new SQLException("删除部门失败，存储过程执行失败，请检查数据库或客户端传入的数据："+id);
    }


    /**
     * 修改部门，只允许修改名字和是否启用
     * @return
     */
    @Override
    public Result<?> updateDepById(Department department) {
        Department dep = new Department().setId(department.getId()).setName(department.getName()).setEnabled(department.getEnabled());
        log.info("需要更新的部门信息：{}",dep);

        int update = departmentMapper.updateById(dep);
        if (1==update){
            return Result.success("修改成功！");
        }
        return Result.error(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()),"修改失败！");
    }
}
